Main Page | Projects IndexGallery Index | Data & Infomation

 

Calculating LRC in VB.Net

Overview

 

The following process describes the steps necessary in order to calculate the LRC value for a string of bytes. A tested program is also provided in the links at the bottom of the page, this was built in VB2017.

LRC is a lot simpler than CRC and can be generated by simply adding all of the byte values together and then subtracting the result from 255.

 

Worked Example

 

A practical worked example was made using the process above:

 

Using above as a guide, a program was then developed that produced a visual printout of the calculation as it is made. A LRC function is also contained within the program. The listing for just the LRC function is given below.


    Public Function lrc(packet As String) As Integer
        'Generate Longitudinal_redundancy_check
        Dim lrc_total As Integer = 0
        Dim byte_int As Integer
        Dim byte_mask As Integer = 255
        Dim byte_data As Integer

        'run though packet to find lrc
        For n = 1 To Len(packet) Step 2
            'scan though string adding each byte
            byte_int = (16 * ah2d(Mid(packet, (n), 1)) + ah2d(Mid(packet, (n + 1), 1)))
            lrc_total = lrc_total + byte_int
            'mask off byte to simulate 8 bit register
          
  lrc_total = lrc_total And byte_mask
        Next n
        'generate final LRC
        lrc_total = (255 - lrc_total) + 1
       
'mask off byte to simulate 8 bit register
        lrc_total = lrc_total And byte_mask
        'pass back result
        lrc = lrc_total
    End Function

 

This function calls upon the function below in order to translate the string hex characters into integers that can be added.


    Public Function ah2d(input As String) As Integer
        'ASCII HEX to DECIMAL
        'Translates 0 - F to 0 - 15
        Dim DataByte As Integer
        'convert the input to ascii
        If input <> "" Then DataByte = Asc(input)
        'convert the ascii to decimal
        If DataByte >= 48 And DataByte <= 57 Then
            ah2d = Asc(input) - 48
        Else
            If DataByte >= 65 And DataByte <= 70 Then
                ah2d = Asc(input) - 55
            Else
                ah2d = 0
            End If
        End If
    End Function

The attached program (see link below) performs the LRC calculation and gives a breakdown of the process, the program was given the string 1103006B0003 and the printout below was produced.

 

Links:

 

 VB2017 LRC Demo Program